package com.sparrow.manage.editor;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.util.FileStore;
import com.sparrow.manage.util.NetFileHandler;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX + "/editor")
@Controller
public class EditorController {

    private static final Logger logger = LoggerFactory.getLogger(EditorController.class);

    private static final FileStore fileStore;

    static {
        String path = "";
        try {
            File directory = new File("");// 参数为空
            path = directory.getCanonicalPath();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        fileStore = new FileStore(path + "/" + EnvironmentVariables.FILE_RESOURCE_ROOT);
    }

    public static final String UPLOAD_IMAGE = "image";

    public static final String UPLOAD_BASE64 = "base64";

    @RequestMapping("index")
    @ResponseBody
    public UploadState index(String action, MultipartFile file, HttpServletRequest request){
        logger.info(action);

        if(StringUtils.isNotBlank(action)){
            switch (action){
                case UPLOAD_IMAGE:
                    return this.uploadFile(file);
                case UPLOAD_BASE64:
                    String base64File = request.getParameter("base64File");
                    return this.uploadBase64(base64File);
            }
        }
        return new UploadState();
    }

    private Base64UploadState uploadBase64(String base64File){
        Base64UploadState state = new Base64UploadState();
        state.setSuccess(false);
        try {
            String filePath = "/" + EnvironmentVariables.FILE_ACCESS_PREFIX + "/" + FileStore.STORE_FOLDER + "/" + fileStore.saveBase64(base64File, "temp.png");

            state.setSuccess(true);
            state.setTitle("");
            state.setType("png");
            state.setUrl(filePath);
            state.setOriginal("");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return state;
    }

    private UploadState uploadFile(MultipartFile file){
        UploadState state = new UploadState();
        state.setSuccess(false);

        if(file != null){
            String fileName = file.getOriginalFilename();
            try {
                String size = NetFileHandler.getFileSizeToDouble(file);
                String filePath = "/" + EnvironmentVariables.FILE_ACCESS_PREFIX + "/" + FileStore.STORE_FOLDER + "/" + fileStore.saveFile(file.getInputStream(), fileName);

                state.setSize(size);
                state.setSuccess(true);
                state.setTitle(fileName);
                state.setUrl(filePath);
                state.setType(fileStore.getSuffix(fileName));

            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }

        return state;
    }

}
